import { invoke } from "@tauri-apps/api"
import { appWindow } from "@tauri-apps/api/window";
import { recv } from "./recv";

async function init() {
    await appWindow.listen<number[]>('recv_event', (event) => {
        const buf = event.payload;
        recv(buf);
    });
}

init();

export const serialPort = {

    isOpen: async () => {
        return await invoke<boolean>('is_open');
    },

    write: async (buf: number[]) => {
        await invoke('write_port', {
            buf
        })
    },

    open: async (name: string) => {
        await invoke('open_port', {
            name
        });
    },

    close: async () => {
        await invoke('close_port');
    },

    activePorts: async () => {
        return await invoke<string[]>('active_ports');
    },
}
